package cn.wanda.utils.modelGroup

import cn.wanda.utils.config
import cn.wanda.utils.config.groupJobTrait
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

class group3 extends groupJobTrait {

    def getGroup(sparkSessionContext: SparkSession): DataFrame = {

        val model3TableMap = new config.TableMap
        val model7TableMap = new config.TableMap

        val jdbcDF = sparkSessionContext.sql(group3Sql)
          .rdd
          .map { col => {
              val memberCode = col.getString(0)
              val lst_od_dob3 = isNull(col.get(1))
              val earlist_shidong_rate = isNull(col.get(2))
              val lst_shidong_rate2 = isNull(col.get(3))
              val lst_txn_dob = isNull(col.get(4))
              val od_avg_day_12mth = isNull(col.get(5))
              val od_intvl_max = isNull(col.get(6))
              val od_tran_cnt_1mth = isNull(col.get(7))
              val rule_els = isNull(col.get(8))
              val td_90d = isNull(col.get(9))
              val bqs_90d = isNull(col.get(10))

              var lst_od_dob3_goal = 0f
              var earlist_shidong_rate_goal = 0f
              var lst_shidong_rate2_goal = 0f
              var lst_txn_dob_goal = 0f
              var od_avg_day_12mth_goal = 0f
              var od_intvl_max_goal = 0f
              var od_tran_cnt_1mth_goal = 0f
              var rule_els_goal = 0f
              var td_90d_goal = 0f
              var bqs_90d_goal = 0f

              if (lst_od_dob3 < 0f) {
                  lst_od_dob3_goal = 8.7f
              } else if (lst_od_dob3 <= 31f) {
                  lst_od_dob3_goal = -59.1f
              } else if (lst_od_dob3 <= 90f) {
                  lst_od_dob3_goal = -19.4f
              } else {
                  lst_od_dob3_goal = 21.4f
              }

              if (earlist_shidong_rate <= 0.8f) {
                  earlist_shidong_rate_goal = 8.1f
              } else if (earlist_shidong_rate <= 0.9f) {
                  earlist_shidong_rate_goal = -4.0f
              } else if (earlist_shidong_rate <= 0.95f) {
                  earlist_shidong_rate_goal = -14.3f
              } else {
                  earlist_shidong_rate_goal = -12.2f
              }

              if (lst_shidong_rate2 <= 0.8f) {
                  lst_shidong_rate2_goal = 11.9f
              } else if (lst_shidong_rate2 <= 0.9f) {
                  lst_shidong_rate2_goal = 1.6f
              } else if (lst_shidong_rate2 <= 0.95f) {
                  lst_shidong_rate2_goal = -6.7f
              } else {
                  lst_shidong_rate2_goal = -17.7f
              }

              if (lst_txn_dob < 0f) {
                  lst_txn_dob_goal = -5.1f
              } else if (lst_txn_dob <= 62f) {
                  lst_txn_dob_goal = -10.4f
              } else {
                  lst_txn_dob_goal = 12.8f
              }

              if (od_avg_day_12mth < 1.3f) {
                  od_avg_day_12mth_goal = 12.3f
              } else if (od_avg_day_12mth < 2f) {
                  od_avg_day_12mth_goal = -4.4f
              } else {
                  od_avg_day_12mth_goal = -22.7f
              }

              if (od_intvl_max <= 31f) {
                  od_intvl_max_goal = -27.7f
              } else if (od_intvl_max <= 61f) {
                  od_intvl_max_goal = -0.7f
              } else {
                  od_intvl_max_goal = 8.8f
              }

              if (od_tran_cnt_1mth <= 1f) {
                  od_tran_cnt_1mth_goal = 6.6f
              } else if (od_tran_cnt_1mth <= 2f) {
                  od_tran_cnt_1mth_goal = 1.6f
              } else if (od_tran_cnt_1mth <= 4f) {
                  od_tran_cnt_1mth_goal = -4.0f
              } else {
                  od_tran_cnt_1mth_goal = -13.6f
              }

              if (rule_els <= 0f) {
                  rule_els_goal = -15.8f
              } else if (rule_els <= 29f) {
                  rule_els_goal = 18.8f
              } else if (rule_els <= 48f) {
                  rule_els_goal = -1.2f
              } else {
                  rule_els_goal = -20.6f
              }

              if (td_90d <= 0f) {
                  td_90d_goal = -4.2f
              } else if (td_90d <= 2f) {
                  td_90d_goal = 7.9f
              } else if (td_90d <= 17f) {
                  td_90d_goal = 3.3f
              } else {
                  td_90d_goal = -7.2f
              }

              val model3Score: Float = 500.00f + lst_od_dob3_goal + earlist_shidong_rate_goal + lst_shidong_rate2_goal + lst_txn_dob_goal + od_avg_day_12mth_goal + od_intvl_max_goal + od_tran_cnt_1mth_goal + rule_els_goal + td_90d_goal

              model3TableMap.member_code = memberCode
              model3TableMap.lst_od_dob3_value = lst_od_dob3
              model3TableMap.earlist_shidong_rate_value = earlist_shidong_rate
              model3TableMap.lst_shidong_rate2_value = lst_shidong_rate2
              model3TableMap.lst_txn_dob_value = lst_txn_dob
              model3TableMap.od_avg_day_12mth_value = od_avg_day_12mth
              model3TableMap.od_intvl_max_value = od_intvl_max
              model3TableMap.od_tran_cnt_1mth_value = od_tran_cnt_1mth
              model3TableMap.rule_els_value = rule_els
              model3TableMap.td_90d_value = td_90d
              model3TableMap.lst_od_dob3_goal = lst_od_dob3_goal
              model3TableMap.earlist_shidong_rate_goal = earlist_shidong_rate_goal
              model3TableMap.lst_shidong_rate2_goal = lst_shidong_rate2_goal
              model3TableMap.lst_txn_dob_goal = lst_txn_dob_goal
              model3TableMap.od_avg_day_12mth_goal = od_avg_day_12mth_goal
              model3TableMap.od_intvl_max_goal = od_intvl_max_goal
              model3TableMap.od_tran_cnt_1mth_goal = od_tran_cnt_1mth_goal
              model3TableMap.rule_els_goal = rule_els_goal
              model3TableMap.td_90d_goal = td_90d_goal
              model3TableMap.model_of_the_total_score = model3Score

              val model3Map: config.TableMap = model3TableMap

              if (lst_od_dob3 < 0f) {
                  lst_od_dob3_goal = 7.5f
              } else if (lst_od_dob3 <= 31f) {
                  lst_od_dob3_goal = -52.4f
              } else if (lst_od_dob3 <= 45f) {
                  lst_od_dob3_goal = -27.2f
              } else {
                  lst_od_dob3_goal = 19.0f
              }

              if (bqs_90d <= 0f) {
                  bqs_90d_goal = 1.6f
              } else {
                  bqs_90d_goal = -107.5f
              }

              if (earlist_shidong_rate <= 0.8f) {
                  earlist_shidong_rate_goal = 7.2f
              } else if (earlist_shidong_rate <= 0.9f) {
                  earlist_shidong_rate_goal = 7.5f
              } else if (earlist_shidong_rate <= 0.95f) {
                  earlist_shidong_rate_goal = -6.2f
              } else {
                  earlist_shidong_rate_goal = -11.2f
              }

              if (lst_shidong_rate2 <= 0.8f) {
                  lst_shidong_rate2_goal = 11.6f
              } else if (lst_shidong_rate2 <= 0.9f) {
                  lst_shidong_rate2_goal = 5.2f
              } else if (lst_shidong_rate2 <= 0.95f) {
                  lst_shidong_rate2_goal = -3.9f
              } else {
                  lst_shidong_rate2_goal = -21.1f
              }

              if (lst_txn_dob < 0f) {
                  lst_txn_dob_goal = -13.4f
              } else if (lst_txn_dob <= 62f) {
                  lst_txn_dob_goal = -14.4f
              } else {
                  lst_txn_dob_goal = 19.4f
              }

              if (od_avg_day_12mth < 1.3f) {
                  od_avg_day_12mth_goal = 8.4f
              } else if (od_avg_day_12mth < 1.7f) {
                  od_avg_day_12mth_goal = 2.2f
              } else if (od_avg_day_12mth < 2.2f) {
                  od_avg_day_12mth_goal = -8.6f
              } else {
                  od_avg_day_12mth_goal = -24.5f
              }

              if (od_intvl_max <= 31f) {
                  od_intvl_max_goal = -30.8f
              } else if (od_intvl_max <= 62f) {
                  od_intvl_max_goal = 3.3f
              } else {
                  od_intvl_max_goal = 10.0f
              }

              if (od_tran_cnt_1mth <= 1f) {
                  od_tran_cnt_1mth_goal = 4.0f
              } else if (od_tran_cnt_1mth <= 2f) {
                  od_tran_cnt_1mth_goal = 1.3f
              } else if (od_tran_cnt_1mth <= 4f) {
                  od_tran_cnt_1mth_goal = -2.7f
              } else {
                  od_tran_cnt_1mth_goal = -8.4f
              }

              if (rule_els <= 0f) {
                  rule_els_goal = -42.9f
              } else if (rule_els <= 29f) {
                  rule_els_goal = 32.2f
              } else if (rule_els <= 48f) {
                  rule_els_goal = 10.5f
              } else {
                  rule_els_goal = -7.5f
              }

              val model7Score: Float = 500.00f + lst_od_dob3_goal + bqs_90d_goal + earlist_shidong_rate_goal + lst_shidong_rate2_goal + lst_txn_dob_goal + od_avg_day_12mth_goal + od_intvl_max_goal + od_tran_cnt_1mth_goal + rule_els_goal

              model7TableMap.member_code = memberCode
              model7TableMap.member_code = memberCode
              model7TableMap.lst_od_dob3_value = lst_od_dob3
              model7TableMap.earlist_shidong_rate_value = earlist_shidong_rate
              model7TableMap.lst_shidong_rate2_value = lst_shidong_rate2
              model7TableMap.lst_txn_dob_value = lst_txn_dob
              model7TableMap.od_avg_day_12mth_value = od_avg_day_12mth
              model7TableMap.od_intvl_max_value = od_intvl_max
              model7TableMap.od_tran_cnt_1mth_value = od_tran_cnt_1mth
              model7TableMap.rule_els_value = rule_els
              model7TableMap.bqs_90d_value = bqs_90d
              model7TableMap.lst_od_dob3_goal = lst_od_dob3_goal
              model7TableMap.earlist_shidong_rate_goal = earlist_shidong_rate_goal
              model7TableMap.lst_shidong_rate2_goal = lst_shidong_rate2_goal
              model7TableMap.lst_txn_dob_goal = lst_txn_dob_goal
              model7TableMap.od_avg_day_12mth_goal = od_avg_day_12mth_goal
              model7TableMap.od_intvl_max_goal = od_intvl_max_goal
              model7TableMap.od_tran_cnt_1mth_goal = od_tran_cnt_1mth_goal
              model7TableMap.rule_els_goal = rule_els_goal
              model7TableMap.bqs_90d_goal = bqs_90d_goal
              model7TableMap.model_of_the_total_score = model7Score

              val model7Map: config.TableMap = model7TableMap
              (model3Map, model7Map)

          }

          }

        val model3Rdd = jdbcDF.map(_._1).map(
            x => Row(x.data_date, 3, x.member_code, null, x.td_90d_value, null, null, x.rule_els_value, null, x.earlist_shidong_rate_value,
                null, x.lst_shidong_rate2_value, null, null, null, x.lst_od_dob3_value, x.lst_txn_dob_value, x.od_intvl_max_value, x.od_tran_cnt_1mth_value, x.od_avg_day_12mth_value,
                null, null, null, null, null, null, null, x.td_90d_goal, null, null,
                x.rule_els_goal, null, x.earlist_shidong_rate_goal, null, x.lst_shidong_rate2_goal, null, null, null, x.lst_od_dob3_goal, x.lst_txn_dob_goal,
                x.od_intvl_max_goal, x.od_tran_cnt_1mth_goal, x.od_avg_day_12mth_goal, null, null, null, null, null, null, x.model_of_the_total_score))

        val model3DF = sparkSessionContext.sqlContext.createDataFrame(model3Rdd, schema)

        val model7Rdd = jdbcDF.map(_._2).map(
            x => Row(x.data_date, 3, x.member_code, null, null, null, x.bqs_90d_value, x.rule_els_value, null, x.earlist_shidong_rate_value,
                null, x.lst_shidong_rate2_value, null, null, null, x.lst_od_dob3_value, x.lst_txn_dob_value, x.od_intvl_max_value, x.od_tran_cnt_1mth_value, x.od_avg_day_12mth_value,
                null, null, null, null, null, null, null, null, null, x.bqs_90d_goal,
                x.rule_els_goal, null, x.earlist_shidong_rate_goal, null, x.lst_shidong_rate2_goal, null, null, null, x.lst_od_dob3_goal, x.lst_txn_dob_goal,
                x.od_intvl_max_goal, x.od_tran_cnt_1mth_goal, x.od_avg_day_12mth_goal, null, null, null, null, null, null, x.model_of_the_total_score))

        val model7DF = sparkSessionContext.sqlContext.createDataFrame(model7Rdd, schema)
        model3DF.union(model7DF)
    }
}
